import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '@/views/Login'
import Register from '@/views/Login/register.vue'
import Student from '@/views/student'
import Teacher from '@/views/teacher'
import Exam from '@/views/teacher/Exam'
import Problem from '@/views/teacher/Problem'
import Grade from '@/views/teacher/Grade'
import GradeClass from '@/views/teacher/Grade/gradeClass'
import Txq from '@/components/Teacher/Txq'
import Sexam from '@/views/student/Sexam'
import Smy from '@/views/student/Smy/MyExam'
import Sxq from '@/components/Student/ExamView'
import exam from '@/views/exam'
import single from '@/views/exam/singleChoice'
import multiple from '@/views/exam/multipleChoice'
import judge from '@/views/exam/judgeChoice'
import Detail from '@/components/Teacher/ExDetail.vue'

Vue.use(VueRouter)
const originalPush = VueRouter.prototype.push

VueRouter.prototype.push = function push (location) {
  return originalPush.call(this, location).catch(err => err)
}
const routes = [
  {
    path: '/login',
    component: Login
  },
  {
    path: '/register',
    component: Register
  },
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/student',
    component: Student,
    redirect: '/student/sexam',
    children: [
      {
        path: 'sexam',
        component: Sexam
      },
      {
        path: 'smy',
        component: Smy
      }
    ]
  },

  {
    path: '/teach',
    component: Teacher,
    redirect: '/teach/problem/questionList',
    children: [
      {
        path: 'problem',
        component: Problem,
        redirect: '/teach/problem/questionList',
        meta: {
          title: '问题管理',
          authority: 'teacher',
          roles: ['老师']
        },
        children: [ // 子路由（问题列表）
          {
            path: 'questionList',
            name: 'QuestionList',
            component: () => import('@/components/Teacher/question/Qs_List.vue'),
            meta: {
              title: '问题列表',
              authority: 'teacher',
              roles: ['老师']
            }
          }
        ]
      },
      {
        path: 'exam',
        component: Exam
      },
      {
        path: 'grade',
        component: Grade,
        redirect: '/teach/grade/gradeView',
        children: [
          {
            path: 'gradeView',
            name: 'gradeView',
            component: () => import('@/views/teacher/Grade/grade.vue')
          },
          {
            path: 'gradeClass',
            name: 'gradeClass',
            component: GradeClass
          },
          {
            path: 'gradeDetail',
            component: Txq,
            children: [
              {
                path: 'MyMultiSelect/:id',
                name: 'MyMultiSelect',
                props: true,
                meta: { title: '考试页面', auth: true },
                component: () => import('@/components/Student/MyMultiSelect')
              },
              {
                path: 'MyRadio/:id',
                name: 'MyRadio',
                props: true,
                meta: { title: '考试页面', auth: true },
                component: () => import('@/components/Student/MyRadio.vue')
              },
              {
                path: 'MyJudge/:id',
                name: 'MyJudge',
                props: true,
                meta: { title: '考试页面', auth: true },
                component: () => import('@/components/Student/MyJudge.vue')
              }
            ]
          }
        ]
      }

    ]
  },
  // 考试页面
  {
    path: '/exam',
    name: 'exam',
    component: exam,
    // meta: { isShow: true },
    beforeEnter: (to, from, next) => {
      if (from.path == '/student/sexam') {
        to.meta.isShow = true
      } else {
        to.meta.isShow = false
      }
      next()
    },
    children: [
      {
        path: 'single/:id',
        name: 'single',
        component: single,
        props: true
      },
      {
        path: 'multiple/:id',
        name: 'multiple',
        component: multiple,
        props: true
      },
      {
        path: 'judge/:id',
        name: 'judge',
        component: judge,
        props: true
      }
      // {
      //   path: '/exam',
      //   redirect: '/exam/single/1'
      // }
    ]
  },
  // 老师查看题目页面
  {
    path: '/detail',
    component: Detail,
    redirect: '/detail/single/1',
    children: [
      {
        path: 'single/:id',
        name: 'single',
        component: single,
        props: true
      },
      {
        path: 'multiple/:id',
        name: 'multiple',
        component: multiple,
        props: true
      },
      {
        path: 'judge/:id',
        name: 'judge',
        component: judge,
        props: true
      }
      // {
      //   path: '/exam',
      //   redirect: '/exam/single/1'
      // }
    ]
  },
  {
    path: '/sxq',
    component: Sxq,
    children: [
      { path: 'MyMultiSelect/:id', name: 'MyMultiSelect', props: true, meta: { title: '考试页面', auth: true }, component: () => import('@/components/Student/MyMultiSelect') },
      { path: 'MyRadio/:id', name: 'MyRadio', props: true, meta: { title: '考试页面', auth: true }, component: () => import('@/components/Student/MyRadio.vue') },
      { path: 'MyJudge/:id', name: 'MyJudge', props: true, meta: { title: '考试页面', auth: true }, component: () => import('@/components/Student/MyJudge.vue') }
    ]
  }
]

const router = new VueRouter({
  routes
})
// const token = sessionStorage.getItem('token')

// router.beforeEach((to, from, next) => {
//   if (to.path == '/login') {
//     next()
//   }
//   if (!token) {
//     next()
//   } else {
//     next('/login')
//   }
// })

export default router
